package com.orange.rd.sophia.android.sensorscharts.tools;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.orange.rd.sophia.android.sensorscharts.R;
import com.orange.rd.sophia.android.sensorscharts.activity.SensorChartActivity;
import com.orange.rd.sophia.android.sensorscharts.activity.SensorsSetupActivity;
import com.orange.rd.sophia.android.sensorscharts.conf.Properties;

/**
 * Manage menu visibility/disability... Launch action
 * 
 * @author VLWB5512
 * 
 */
public class ApplicationMenu {
	public static void onCreateOptionsMenu(Activity context, Menu menu) {
		MenuInflater inflater = context.getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
	}

	public static void onPrepareOptionsMenu(Activity context, Menu menu) {

		menu.findItem(R.id.quit).setVisible(true);

		if (context.getClass().getSimpleName().equals(SensorChartActivity.class.getName())) {
			menu.findItem(R.id.options).setVisible(true);
			menu.findItem(R.id.setup).setVisible(true);
			menu.findItem(R.id.filter).setVisible(true);

			if (context.getClass().getSimpleName().equals(SensorChartActivity.TAG)) {
				menu.findItem(R.id.sensor).setVisible(true);
				menu.findItem(R.id.graph).setVisible(false);
			} else {
				menu.findItem(R.id.sensor).setVisible(false);
				menu.findItem(R.id.graph).setVisible(true);
			}
		}
	}

	public static void onOptionsItemSelected(final Activity context,
			MenuItem item) {
		switch (item.getItemId()) {
		case R.id.setup:
			Intent k = new Intent();
			k.setClass(context, SensorsSetupActivity.class);
			k.setAction(Intent.ACTION_VIEW);
			Properties.previousActivity = context.getClass();
			context.finish();
			context.startActivity(k);
			break;
		case R.id.options:
			AlertDialog.Builder builder0 = new AlertDialog.Builder(context);
			builder0.setCancelable(true);
			builder0.setTitle(context.getString(R.string.options));
			boolean[] selection = null;
			if (context.getClass().getSimpleName().equals(SensorChartActivity.TAG)) {
				// Graph options
				selection = new boolean[11];
				selection[0]=Properties.showPoints;
				selection[1]=Properties.showLines;
				selection[2]=Properties.showPhoneOrientation;
				selection[3]=Properties.showPhoneRotation;
				selection[4]=Properties.showX;
				selection[5]=Properties.showY;
				selection[6]=Properties.showZ;
				selection[7]=Properties.showComputedData;
				selection[8]=Properties.saveDataInLocalFile;
				selection[9]=Properties.calibrateGyroscope;
				selection[10]=Properties.calibrateLinearAccel;
				final CharSequence[] choiceGraph = {
						context.getString(R.string.show_graph_points),
						context.getString(R.string.show_graph_lines),
						context.getString(R.string.show_mobile_orientation), 
						context.getString(R.string.show_mobile_rotation), 
						context.getString(R.string.show_x), 
						context.getString(R.string.show_y), 
						context.getString(R.string.show_z), 
						context.getString(R.string.show_computed_data), 
						context.getString(R.string.save_data_in_local_file),
						context.getString(R.string.calibrate_gyroscope),
						context.getString(R.string.calibrate_linear_accel)
				};
				builder0.setMultiChoiceItems(choiceGraph, selection, new DialogInterface.OnMultiChoiceClickListener() {
					public void onClick(DialogInterface dialog, int which, boolean isChecked) {
						switch (which) {
						case 0:
							Properties.showPoints = isChecked;
							break;
						case 1:
							Properties.showLines = isChecked;
							break;
						case 2:
							Properties.showPhoneOrientation = isChecked;
							break;
						case 3:
							Properties.showPhoneRotation = isChecked;
							break;
						case 4:
							Properties.showX = isChecked;
							break;
						case 5:
							Properties.showY = isChecked;
							break;
						case 6:
							Properties.showZ = isChecked;
							break;
						case 7:
							Properties.showComputedData = isChecked;
							break;
						case 8:
							Properties.saveDataInLocalFile = isChecked;
							break;
						case 9:
							Properties.calibrateGyroscope = isChecked;
							break;
						case 10:
							Properties.calibrateLinearAccel = isChecked;
							break;
						}
					}
				});
				AlertDialog alert0 = builder0.create();
				alert0.show();
			}
			break;
		case R.id.filter:
			AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
			builder1.setCancelable(true);
			builder1.setTitle(context.getString(R.string.filter_type));
			final CharSequence[] choiceList = {
					context.getString(R.string.none),
					context.getString(R.string.high_pass),
					context.getString(R.string.high_pass_with_adaptative),
					context.getString(R.string.low_pass),
					context.getString(R.string.low_pass_with_adaptative),
					context.getString(R.string.kalman) };
			int selected = 0; // None
			switch (SensorsDataManager.selectedData.getFilterType()) {
			case SensorsDataManager.TYPE_FILTER_NONE:
				selected = 0;
				break;
			case SensorsDataManager.TYPE_FILTER_HIGH_PASS:
				if (!SensorsDataManager.selectedData.isWithAdaptativeFilter())
					selected = 1;
				else
					selected = 2;
				break;
			case SensorsDataManager.TYPE_FILTER_LOW_PASS:
				if (!SensorsDataManager.selectedData.isWithAdaptativeFilter())
					selected = 3;
				else
					selected = 4;
				break;
			case SensorsDataManager.TYPE_FILTER_KALMAN:
				selected = 5;
				break;
			}

			builder1.setSingleChoiceItems(choiceList, selected,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							switch (which) {
							case 0:
								SensorsDataManager.setSensorDataFilterType(
										SensorsDataManager.TYPE_FILTER_NONE,
										false);
								break;
							case 1:
								SensorsDataManager
										.setSensorDataFilterType(
												SensorsDataManager.TYPE_FILTER_HIGH_PASS,
												false);
								break;
							case 2:
								SensorsDataManager
										.setSensorDataFilterType(
												SensorsDataManager.TYPE_FILTER_HIGH_PASS,
												true);
								break;
							case 3:
								SensorsDataManager.setSensorDataFilterType(
										SensorsDataManager.TYPE_FILTER_LOW_PASS,
										false);
								break;
							case 4:
								SensorsDataManager.setSensorDataFilterType(
										SensorsDataManager.TYPE_FILTER_LOW_PASS,
										true);
								break;
							case 5:
								SensorsDataManager.setSensorDataFilterType(
										SensorsDataManager.TYPE_FILTER_KALMAN,
										false);
								break;
							}
							dialog.dismiss();
						}
					});
			AlertDialog alert1 = builder1.create();
			alert1.show();
			break;
		case R.id.sensor:
			AlertDialog.Builder builder2 = new AlertDialog.Builder(context);
			builder2.setCancelable(true);
			builder2.setTitle(context.getString(R.string.sensor_type));
			final CharSequence[] choiceList2 = {
					SensorsDataManager
							.getSensorTypeName(SensorsDataManager.SENSOR_TYPE_ACCELEROMETER),
					SensorsDataManager
							.getSensorTypeName(SensorsDataManager.SENSOR_TYPE_ACCELEROMETER_WITHOUT_GRAVITY),
					SensorsDataManager
							.getSensorTypeName(SensorsDataManager.SENSOR_TYPE_GRAVITY),
					SensorsDataManager
							.getSensorTypeName(SensorsDataManager.SENSOR_TYPE_GYROSCOPE),
					SensorsDataManager
							.getSensorTypeName(SensorsDataManager.SENSOR_TYPE_LINERA_ACCEL),
					SensorsDataManager
							.getSensorTypeName(SensorsDataManager.SENSOR_TYPE_MAGNETIC_FIELD),
					SensorsDataManager
							.getSensorTypeName(SensorsDataManager.SENSOR_TYPE_ROTATION_VECTOR),
					SensorsDataManager
							.getSensorTypeName(SensorsDataManager.SENSOR_TYPE_ORIENTATION) };
			int selected2 = 0; // Accelerometer
			switch (SensorsDataManager.selectedData.getSensorType()) {
			case SensorsDataManager.SENSOR_TYPE_ACCELEROMETER:
				selected2 = 0;
				break;
			case SensorsDataManager.SENSOR_TYPE_ACCELEROMETER_WITHOUT_GRAVITY:
				selected2 = 1;
				break;
			case SensorsDataManager.SENSOR_TYPE_GRAVITY:
				selected2 = 2;
				break;
			case SensorsDataManager.SENSOR_TYPE_GYROSCOPE:
				selected2 = 3;
				break;
			case SensorsDataManager.SENSOR_TYPE_LINERA_ACCEL:
				selected2 = 4;
				break;
			case SensorsDataManager.SENSOR_TYPE_MAGNETIC_FIELD:
				selected2 = 5;
				break;
			case SensorsDataManager.SENSOR_TYPE_ROTATION_VECTOR:
				selected2 = 6;
				break;
			case SensorsDataManager.SENSOR_TYPE_ORIENTATION:
				selected2 = 7;
				break;
			}
			builder2.setSingleChoiceItems(choiceList2, selected2,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							SensorsDataManager.selectedData
									.removeObserver((SensorObserverInterface) context);
							switch (which) {
							case 0 | 1:
								SensorsDataManager.selectedData = SensorsDataManager.accelerometerData;
								if (which==0) {
									SensorsDataManager.selectedData.isWithAdaptativeFilter(false);
									SensorsDataManager.selectedData.setSensorType(SensorsDataManager.SENSOR_TYPE_ACCELEROMETER);
								} else {
									SensorsDataManager.selectedData.isWithAdaptativeFilter(true);
									SensorsDataManager.selectedData.setSensorType(SensorsDataManager.SENSOR_TYPE_ACCELEROMETER_WITHOUT_GRAVITY);
								}
								break;
							case 2:
								SensorsDataManager.selectedData = SensorsDataManager.gravityData;
								break;
							case 3:
								SensorsDataManager.selectedData = SensorsDataManager.gyroscopeData;
								break;
							case 4:
								SensorsDataManager.selectedData = SensorsDataManager.linearAccelData;
								break;
							case 5:
								SensorsDataManager.selectedData = SensorsDataManager.magneticFieldData;
								break;
							case 6:
								SensorsDataManager.selectedData = SensorsDataManager.rotationData;
								break;
							case 7:
								SensorsDataManager.selectedData = SensorsDataManager.orientationData;
								break;
							}
							SensorsDataManager.selectedData
									.addObserver((SensorObserverInterface) context);
							dialog.dismiss();
						}
					});
			AlertDialog alert2 = builder2.create();
			alert2.show();
			break;
		case R.id.graph:
			Intent i = new Intent();
			i.setClass(context, SensorChartActivity.class);
			i.setAction(Intent.ACTION_VIEW);
			context.finish();
			context.startActivity(i);
			break;
		case R.id.quit:
			context.finish();
			android.os.Process.killProcess(android.os.Process.myPid());
			java.lang.System.exit(0);
			break;
		}
	}
}
